package main.java.lock;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class AtomicDemo implements Runnable {

    // 共享全局变量
    AtomicInteger atomicInteger = new AtomicInteger();

    static long time1;
    static long time2;

    @Override
    public void run() {
        while (true) {
            Integer count = getCount();
            if (count >= 1000) {
                time2 = System.currentTimeMillis();
                int time = (int) ((time2 - time1)/1000);
                System.out.println("执行了："+time+"秒！");
                break;
            }
            System.out.println(count);
        }
    }

    private Integer getCount() {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return atomicInteger.incrementAndGet();
    }

    public static void main(String[] args) {
        time1 = System.currentTimeMillis();
        AtomicDemo useAtomic = new AtomicDemo();
        for (int index = 0; index < 10; index++) {
            Thread t = new Thread(useAtomic);
            t.start();
        }
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
    }
}
